package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.core.util.NJCodeUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerConstant;
import net.dgg.dggcloud.service.jrcrm.dao.cms.SmsRecordDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.SmsRecord;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Contact;
import net.dgg.dggcloud.service.jrcrm.utils.BizExceptionUtil;
import net.dgg.dggcloud.service.jrcrm.utils.SmsBaiwu;
import net.dgg.dggcloud.service.jrcrm.utils.SmsManDao;
import net.dgg.dggcloud.service.jrcrm.utils.SmsMarketingDao;
import net.dgg.dggcloud.service.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: SmsRecordService
 * @Description: 短信记录
 * @author：guowei
 * @date： 2017/11/6
 */

@Service
public class SmsRecordService extends BaseService {

    //注入NJCodeUtil
    @Autowired
    private NJCodeUtil nJCodeUtil;

    @Autowired
    private SmsRecordDao smsRecordDao;

    @Autowired
    private UserService userService;
    /**
     * 根据客户电话号码 查询短信记录
     *
     * @param map
     *   phone 电话号码
     * @return
     */
    public List<Map> listSmsRecordByMap(Map map) {
        return smsRecordDao.listSmsRecordByMap(map);
    }

    /**
     * 根据客户电话号码 查询短信记录 数量
     *
     * @param map
     *    phone 电话号码
     * @return
     */
    public Integer countSmsRecordByMap(Map map) {
        return smsRecordDao.countSmsRecordByMap(map);
    }

    /**
     * 根据客户电话号码 查询短信记录
     *
     * @param map
     *   phone 电话号码
     * @return
     */
    public List<Map> isListSmsRecordByMap(Map map) {
        return smsRecordDao.isListSmsRecordByMap(map);
    }

    /**
     * 根据客户电话号码 查询短信记录 数量
     *
     * @param map
     *    phone 电话号码
     * @return
     */
    public Integer isCountSmsRecordByMap(Map map) {
        return smsRecordDao.isCountSmsRecordByMap(map);
    }


    /**
     *  行业短信,用于给商务发送提醒
     * @param mobile 电话号码 多个号码 ，分开
     * @param content  内容
     * @param name  接收人名称
     * @param sendUser 发送人
     * @return
     */
    public void sendMsg(String mobile, String content,String name,User sendUser) {
        try{
            String[] numbers = mobile.split(",");
            if(null != numbers && numbers.length > 0){
                for (int i = 0; i <numbers.length ; i++) {
                    String resultStr = SmsBaiwu.sendMsg(mobile,content);
                    SmsRecord smsRecord = new SmsRecord();
                    smsRecord.setId(keyWorker.nextId());
                    smsRecord.setName(name);
                    smsRecord.setContent(content);
                    smsRecord.setPhone(numbers[i]);
                    smsRecord.setCreateUser(sendUser);
                    smsRecord.setResultStr(resultStr);
                    smsRecord.setStatus(1);
                    this.repository.save(smsRecord);
                }
            }
        }catch (Exception e){
        }
    }

    /**
     * 发送短信给客户的联系人
     * @param contactId 联系人id
     * @param content 内容
     * @param currentLoginUser 发送人
     */
    public void sendMsgToCustomer(Long contactId, String content, User currentLoginUser) {
        BizExceptionUtil.isException(null==currentLoginUser,"发送人不能为空");
        BizExceptionUtil.isException(StringUtils.isEmpty(content),"短信内容不能为空");
        BizExceptionUtil.isException(null==contactId,"接收人不能为空");
        Contact contact = repository.get(Contact.class,contactId);
        BizExceptionUtil.isException(null==contact || CustomerConstant.CUS_CONTACT_STATUS_0.equals(contact.getStatus()) || !"MB".equals(contact.getContactWay()),"未找到短信接收人");
        //漫道发送短信
        //String result = new SmsManDao().sendMsg(contact.getNo(),content);
        //营销发短信
        String result = new SmsMarketingDao().sendMsg(nJCodeUtil.decrypt(contact.getNo()),content);
        SmsRecord smsRecord = new SmsRecord();
        smsRecord.setId(keyWorker.nextId());
        smsRecord.setName(contact.getName());
        smsRecord.setContent(HtmlUtils.htmlEscape(content));
        //需要对电话号码解密
        smsRecord.setPhone(nJCodeUtil.decrypt(contact.getNo()));
        smsRecord.setCreateUser(currentLoginUser);
        smsRecord.setResultStr(result);
        smsRecord.setStatus(1);
        this.repository.save(smsRecord);

    }
}
